/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.ui.dialog.regular;

import com.toedter.calendar.JDateChooser;
import kz.kstu.ent.app.delegate.api.StudentDelegate;
import kz.kstu.ent.app.delegate.impl.StudentDelegateImpl;
import kz.kstu.ent.business.entity.ResultSheet;
import kz.kstu.ent.business.entity.Student;
import kz.kstu.ent.business.entity.dto.RegularTestingDto;
import kz.kstu.ent.desktop.images.GUIFactory;
import kz.kstu.ent.desktop.images.GUIIcon;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Date: 02.09.12
 * Time: 9:01
 *
 * @author Oleg Anastassov
 */
public class RegularTestingDialog extends JDialog {

    private static final String TITLE = "Тестирование :: Очередная сдача ЕНТ";

    private static final int WIDTH = 900;
    private static final int HEIGHT = 600;

    private int MAX = 20;

    private DefaultBoundedRangeModel model;

    private JTable resultsTable;
    private JButton saveButton;
    private JButton closeButton;
    private JPanel rootPanel;
    private JDateChooser dateChooser;

    private JComboBox subjectComboBox;

    private RegularTestingTableModel tableModel;

    public RegularTestingDialog() {
        setIconImage(
                GUIFactory.getImage(GUIIcon.REGULAR_ENT)
        );
        tableModel = new RegularTestingTableModel();
    }

    public void initStaticComponents() {
        setContentPane(rootPanel);
        setModal(true);
        setSize(WIDTH, HEIGHT);
        resultsTable.setModel(new RegularTestingTableModel());
        setResizable(false);
        resultsTable.getTableHeader().setReorderingAllowed(false);
//        resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//        TableColumnModel columnModel = allStudentsTable.getColumnModel();
//        TableColumn idColumn = columnModel.getColumn(AbstractNumeratedTableModel.ID_COLUMN_INDEX);
//        idColumn.setMinWidth(0);
//        idColumn.setMaxWidth(0);
        setTitle(TITLE);


        initIcons();
        initListeners();
        initCellEditors();
    }

    public void initData() {
        subjectComboBox = new JComboBox(
                new String[]{
                        "один", "два"
                }
        );

        StudentDelegate delegate = new StudentDelegateImpl();
        List<Student> allStudents = delegate.findAll(Student.class);

        List<RegularTestingDto> dtoList = new ArrayList<RegularTestingDto>();
        for (Student student : allStudents) {
            RegularTestingDto dto = new RegularTestingDto();

            ResultSheet sheet = new ResultSheet();
            sheet.setStudent(student);

            dto.setStudent(student);
            dto.setResultSheet(sheet);
            dtoList.add(dto);
        }

        tableModel.setData(dtoList);
        resultsTable.setModel(tableModel);

        DefaultCellEditor subjectCellEditor = new DefaultCellEditor(subjectComboBox);
        resultsTable.getColumnModel().getColumn(6).setCellEditor(subjectCellEditor);

    }

    private void initCellEditors() {

    }

    private void initListeners() {
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (dateChooser.getDate() == null) {
                    JOptionPane.showMessageDialog(
                            getRootPane(),
                            "Некорректная дата",
                            "Ошибка",
                            JOptionPane.ERROR_MESSAGE
                    );
                    return;
                }

                // if is Ok
                save();

            }
        });
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    private void save() {

//        dateChooser.setEnabled(false);
//        saveButton.setEnabled(false);
//        resultsTable.setEnabled(false);
//
//        for (int i = 1; i <= resultsTable.getRowCount(); i++) {
//
//            try {
//                Thread.sleep((int) (Math.random() * 1000));
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//
//        }
        resultsTable.setEnabled(true);
        saveButton.setEnabled(true);
        dateChooser.setEnabled(true);
    }

    private void initIcons() {
        saveButton.setIcon(
                GUIFactory.getIcon(GUIIcon.SAVE)
        );
        closeButton.setIcon(
                GUIFactory.getIcon(GUIIcon.CANCEL)
        );
    }

}
